// xc_lshop/shop/deposit/send/com/prolist.js
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    
  },

  /**
   * 组件的初始数据
   */
  data: {
    show:false,
    chooselists:{
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //初始化
    init(dconfig,shoppros){
      this.setData({
        show:true,
        dconfig:dconfig,
        chooselists:shoppros
      })
      this.getlist(dconfig)
    },
    //显示关闭属性
    showattr(e){
      var index=e.currentTarget.dataset.index;
      var name='modellists['+index+"].showattribute";
      var showattribute=this.data.modellists[index].showattribute;
      this.setData({
        [name]:!showattribute
      })
    },
    //单属性增加
    add(e){
      var chooselists=this.data.chooselists
      // console.log("e")
      // console.log(e)
      var index=e.currentTarget.dataset.index;
      // console.log(index)
      // var name='modellists['+index+"].num";
      // console.log(name)
      var modellistitem=this.data.modellists[index]
      // var num=0;
      if(chooselists[modellistitem['id']]){
        chooselists[modellistitem['id']]['num']=chooselists[modellistitem['id']]['num'] + 1
      }else{
        modellistitem['num']=1 
        chooselists[modellistitem['id']]=modellistitem
      }
     
      this.setData({
        'chooselists':chooselists
      })
      console.log("ddddddddddddddddddd")
      console.log(this.data.chooselists)
    },
    //单属性计步器
    onChange(e){
      var chooselists=this.data.chooselists
      var num=e.detail;
      var id=e.currentTarget.dataset.id;
      if(num>0){
        chooselists[id]['num']=num
      }else{
         delete chooselists[id]
      }
      this.setData({
        chooselists:chooselists
      })
    },
    //多规格计步器
    onGuigeChange(e){
      var dataset=e.currentTarget.dataset
      var chooselists=this.data.chooselists
      var modellistitem=this.data.modellists[dataset.index]
      var num=e.detail
      if(chooselists[modellistitem['id']]){ //存在
        modellistitem=chooselists[modellistitem['id']]
        modellistitem['skudata']['stockdata'][dataset.cartesidx]['num']=num
      }else{//不存在
        modellistitem['skudata']['stockdata'][dataset.cartesidx]['num']=num
      }
      var stockdata=modellistitem['skudata']['stockdata'];
      var allnum=0
      for(var i in stockdata){
        if(stockdata[i]['num']>0){
    
          allnum +=stockdata[i]['num'] * 1
        }
      }
      if(allnum>0){
          modellistitem['num']=allnum
          chooselists[modellistitem['id']]=modellistitem
          
      }else{
        delete chooselists[modellistitem['id']]
      }
      this.setData({
        chooselists:chooselists
      })
   

    },
    //关闭页面
    back(){
      this.setData({
        show:false
      })
    },
    //搜索
    onSearch(e){
      console.log("e")
      console.log(e)
      this.setData({
        seachval:e.detail
      })
      this.getlist(this.data.dconfig)
    },
    //清空
    onClear(){
      this.setData({
        seachval:""
      })
      this.getlist(this.data.dconfig)
    },
    //查询列表
    getlist(dconfig){
      var contents=dconfig.contents;
      var pros=contents.pros
      var newpro=[]
      if(this.data.seachval){
         for(var i in pros){
           if(pros[i]['name'].match(this.data.seachval)){
            newpro.push(pros[i])
           }
         }
      }else{
        newpro=pros
      }
      console.log(contents)
      this.setData({
        htdids:contents.htdids,
        modellists:newpro
      }) 
    },
    //确认选择
    onComfirm(){
      var myEventDetail = this.data.chooselists // detail对象，提供给事件监听函数
      var myEventOption = {} // 触发事件的选项
      this.triggerEvent('comfirmbtn', myEventDetail, myEventOption)
      this.back()
    }
  }
})
